package com.utils;

import java.math.BigDecimal;
import java.sql.Timestamp;

/**
 * Created by taoqi on 28/02/2017.
 */
public class TypeUtil {

    public static String strValue(Object obj) {
        return (obj == null) ? null : obj.toString();
    }
    
    public static BigDecimal bigDecimalValue(Object obj) {
    	if (obj == null) {
    		return new BigDecimal(0);
    	}
        return new BigDecimal(String.valueOf(obj));
    }

    public static Long timestampValue(Object obj) {
        if (null == obj || !(obj instanceof Timestamp)) return 0l;
        try {
            return ((Timestamp)obj).getTime();
        } catch (Exception e) {
            return 0l;
        }
    }

    public static Long longValue(Object obj) {
        if (null == obj) return 0l;
        try {
        	return new BigDecimal(String.valueOf(obj)).longValue();
        } catch (Exception e) {
            return 0l;
        }
    }

    public static Integer integerValue(Object obj) {
        if (null == obj) return 0;
        try {
            return new BigDecimal(String.valueOf(obj)).intValue();
        } catch (Exception e) {
            return 0;
        }
    }
    
    public static void main(String[] args) {
    	System.out.println(TypeUtil.longValue(7.1));
		
	}

    public static Float floatValue(Object obj) {
        if (null == obj) return 0f;
        try {
            return Float.parseFloat(String.valueOf(obj));
        } catch (Exception e) {
            return 0f;
        }
    }

    public static Double doubleValue(Object obj) {
        if (null == obj) return 0d;
        try {
            return Double.parseDouble(String.valueOf(obj));
        } catch (Exception e) {
            return 0d;
        }
    }
}
